package zhklappoint.controller;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import zhklappoint.annotation.LoginUser;
import zhklappoint.entity.WXUser;
import zhklappoint.enums.ENUM_SUCESS;
import zhklappoint.service.WXUserService;
import zhklappoint.util.ResponseBean;
import zhklappoint.util.SendMsg;

/***
 * 后台接口
 * @author eagle
 *
 */
@Controller
@CrossOrigin
@RequestMapping(value="/uni/api",produces = "text/html;charset=UTF-8")
public class PhoneMessageController {
	
	@Autowired
	private WXUserService userService;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
				
	//手机验证码验证
    @RequestMapping(value="/checkAuthCode",method = RequestMethod.POST ,produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String checkAuthCode(String phone,@LoginUser WXUser user) {
    	if(ObjectUtils.isEmpty(phone)) {
    		return ResponseBean.of(null,ENUM_SUCESS.FAILED.value , "参数错误");
    	}
    	WXUser u=new WXUser();
    	u.setMobile(phone);
    	Page<WXUser> pusers = userService.findByCondition(u, 1, 10);
    	if(pusers.getTotalElements()>0) {
    		return ResponseBean.of(null,ENUM_SUCESS.FAILED.value , "手机号被占用");
    	}
    	Random r = new Random();
		String code="";
		for(int i=0;i<6;i++) {
			if(i==0) {
				String codeCheck=r.nextInt(10)+"";
				if(!"0".equals(codeCheck)) {
					code+=codeCheck;
				}else {
					code+=5;
				}
			}else {
				code+=r.nextInt(10)+"";
			}
		}
		int times=0;
		Object aucode = stringRedisTemplate.opsForValue().get("authCode:"+phone+":times:");
		if(ObjectUtils.isEmpty(aucode)) {
			times=1;
			stringRedisTemplate.opsForValue().set("authCode:"+phone+":times:","1", 24*60*60,TimeUnit.SECONDS);
		}else {
			times+=1;
			stringRedisTemplate.boundValueOps("authCode:"+phone+":times:").increment(1);
		}
		if(times>5) {
			return ResponseBean.of(null,ENUM_SUCESS.FAILED.value , "短信量达上限");
		}
		stringRedisTemplate.opsForValue().set("authCode:"+phone+":"+user.getId()+":",code, 30*60,TimeUnit.SECONDS);
		SendMsg.sendPhoneMsg(phone, code, "register");
		return ResponseBean.of(null,ENUM_SUCESS.SUCESS.value,"验证码获取成功，请查看手机号");
    }

//手机验证码确认
@RequestMapping(value="/configAuthCode",method = RequestMethod.POST ,produces = "text/html;charset=UTF-8")
@ResponseBody
public String configAuthCode(String phone,HttpServletRequest request,String authCode,@LoginUser WXUser user) {
	Object aucode = stringRedisTemplate.opsForValue().get("authCode:"+phone+":"+user.getId()+":");
	if(ObjectUtils.isEmpty(aucode)) {
		return ResponseBean.of(null, ENUM_SUCESS.FAILED.value,"验证码过期");
	}
	String acode = aucode.toString();
	if(!authCode.equals(acode)) {
		return ResponseBean.of(null, ENUM_SUCESS.FAILED.value,"验证码错误");
	}
	user.setMobile(phone);
	userService.save(user);
	return ResponseBean.of(null,ENUM_SUCESS.SUCESS.value,"验证码验证成功");
}

}